<?php
// 连接数据库
// 引用数据库连接文件
// 添加房间
require_once 'databaseConnection.php';

try {
    // 可以使用 $dbConn 执行数据库操作
    $dbConn = connectToDatabase();

    // 获取表单数据
    $roomNumber = $_POST['roomNumber'];
    $roomType = $_POST['roomType'];
    $roomDescription = $_POST['roomDescription'];
    $price = $_POST['price'];
    $roomService = $_POST['roomService'];
    $roomIntroduction = $_POST['roomIntroduction'];
    $capacity = $_POST['capacity'];
    $fileInput = $_POST['fileInput'];

    // 检查房间号是否已经存在
    $sqlCheck = "SELECT COUNT(*) FROM rooms WHERE roomNumber = :roomNumber";
    $stmtCheck = $dbConn->prepare($sqlCheck);
    $stmtCheck->bindParam(':roomNumber', $roomNumber);
    $stmtCheck->execute();
    $count = $stmtCheck->fetchColumn();
    if ($count > 0) {
        echo '<script>alert("该房间号已存在，请检查");</script>';
        // 终止脚本执行
        exit;
        // 输出错误信息并终止脚本执行
        // 输出错误信息并终止脚本执行
        //die("该房间号已存在，请检查");

       

        
    }

    // 准备SQL语句
    $sql = "INSERT INTO rooms (roomNumber, roomType, roomStatus, price, capacity, fileInput, roomService, roomDescription, roomIntroduction) VALUES (:roomNumber, :roomType, :roomStatus, :price, :capacity, :fileInput, :roomService, :roomDescription, :roomIntroduction)";
    $stmt = $dbConn->prepare($sql);
    $stmt->bindParam(':roomNumber', $roomNumber);
    $stmt->bindParam(':roomType', $roomType);
    $roomStatus = "可用"; // 设置房间状态为可用
    $stmt->bindParam(':roomStatus', $roomStatus);
    $stmt->bindParam(':price', $price);
    $stmt->bindParam(':capacity', $capacity);
    $stmt->bindParam(':fileInput', $fileInput);
    $stmt->bindParam(':roomService', $roomService);
    $stmt->bindParam(':roomDescription', $roomDescription);
    $stmt->bindParam(':roomIntroduction', $roomIntroduction);

    // 执行SQL语句
    try {
        $stmt->execute();
        echo "数据插入成功";
    } catch (PDOException $e) {
        echo "数据插入失败: " . $e->getMessage();
    }

    // 关闭数据库连接
    $dbConn = null;
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}
